
package cn.ddiancan.schedule.listener;

import cn.ddiancan.schedule.task.service.SchedulerService;
import cn.ddiancan.xddcloud.ddccore.cache.XddCloudCacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Set;

/**
 * 刷新任务监听，主要服务增量数据
 *
 * @author Amos
 */
@Service
public class ScheduleRefreshTaskListener implements ApplicationListener {
    @Autowired
    private SchedulerService schedulerService;
    @Autowired
    private XddCloudCacheClient xddCloudCacheClient;

    /**
     * 监听主体
     *
     * @param event
     */
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationReadyEvent) {
            Set<String> keys = xddCloudCacheClient.keys("SCHEDULE_*");
            if(!CollectionUtils.isEmpty(keys)){
                xddCloudCacheClient.delete(new ArrayList<>(keys));
            }
            schedulerService.refreshTaskTimer();
        }
    }
}
